var moduleNo = getQueryString("moduleNo")
$(function () {
    $("#jqGrid").Grid({
        url: '../modulemaintainrecord/list?moduleNo=' + moduleNo,
        colModel: [
            {label: 'id', name: 'id', index: 'ID', key: true, hidden: true},
            {label: '车间', name: 'workshop', index: 'WORKSHOP', width: 80},
            {label: '模具号', name: 'moduleNo', index: 'MODULE_NO', width: 80},
            {label: '维护频次', name: 'maintainFrequency', index: 'MAINTAIN_FREQUENCY', width: 80},
            {
                label: '上次维护日期', name: 'lastDate', index: 'LAST_DATE', width: 80, formatter: function (value) {
                    return transDate(value, 'yyyy-MM-dd')
                }
            },
            {
                label: '计划维护日期', name: 'planDate', index: 'PLAN_DATE', width: 80, formatter: function (value) {
                    return transDate(value, 'yyyy-MM-dd')
                }
            },
            {label: '模具类型', name: 'moduleTypeName', index: 'MODULE_TYPE', width: 80},
            {label: '产品系列', name: 'productSeries', index: 'PRODUCT_SERIES', width: 80},
            {label: '产品类型', name: 'productType', index: 'PRODUCT_TYPE', width: 80},
            {
                label: '是否上传记录',
                name: 'isUploadRecord',
                index: 'IS_UPLOAD_RECORD',
                width: 80,
                formatter: function (value) {
                    return sysEnums.YES_OR_NO[value]
                }
            },
            {label: '备注', name: 'remark', index: 'REMARK', width: 80},
            /* {label: '修改时间', name: 'updateTime', index: 'UPDATE_TIME', width: 80},
            {label: '修改人', name: 'updateUser', index: 'UPDATE_USER', width: 80},
            {label: '创建时间', name: 'createTime', index: 'CREATE_TIME', width: 80},
            {label: '创建人', name: 'createUser', index: 'CREATE_USER', width: 80},
            {label: '删除标识', name: 'delFlag', index: 'DEL_FLAG', width: 80},*/
        ]
    });
});
var setting = {
    data: {
        simpleData: {
            enable: true,
            idKey: "id",
            pIdKey: "superType",
            rootPId: 0
        },
        key: {
            url: "nourl",
            name: "typeName" //name 与 typeName 绑定
        },
    }
};
var moduleTypeTree;
var vm = new Vue({
    el: '#rrapp',
    data: {
        showList: true,
        title: null,
        moduleMaintainRecord: {
            updateTime: '',
            updateUser: '',
            createTime: '',
            createUser: '',
            delFlag: '',
            remark: '',
            isUploadRecord: '',
            productType: '',
            productSeries: '',
            moduleType: '',
            moduleTypeName: '',
            planDate: '',
            lastDate: '',
            maintainFrequency: '',
            moduleNo: '',
            workshop: '',
            id: '',
        },
        ruleValidate: {
            name: [
                {required: true, message: '名称不能为空', trigger: 'blur'}
            ]
        },
        q: {
            name: ''
        }
    },
    methods: {
        query: function () {
            vm.reload();
        },
        add: function () {
            vm.showList = false;
            vm.title = "新增";
            vm.moduleMaintainRecord = {
                updateTime: '',
                updateUser: '',
                createTime: '',
                createUser: '',
                delFlag: '1',
                remark: '',
                isUploadRecord: 0,
                productType: '',
                productSeries: '',
                moduleTypeName: '',
                moduleType: '',
                planDate: '',
                lastDate: '',
                maintainFrequency: '',
                moduleNo: moduleNo,
                workshop: '',
                id: '',
            };
            vm.getModuleTypeTree()
        },
        update: function (event) {
            var id = getSelectedRow("#jqGrid");
            if (id == null) {
                return;
            }
            vm.showList = false;
            vm.title = "修改";

            vm.getInfo(id);
            vm.getModuleTypeTree()
        },
        saveOrUpdate: function (event) {
            var url = vm.moduleMaintainRecord.id == (null || '') ? "../modulemaintainrecord/save" : "../modulemaintainrecord/update";
            Ajax.request({
                url: url,
                params: JSON.stringify(vm.moduleMaintainRecord),
                type: "POST",
                contentType: "application/json",
                successCallback: function (r) {
                    alert('操作成功', function (index) {
                        vm.reload();
                    });
                }
            });
        },
        del: function (event) {
            var ids = getSelectedRows("#jqGrid");
            if (ids == null) {
                return;
            }

            confirm('确定要删除选中的记录？', function () {
                Ajax.request({
                    url: "../modulemaintainrecord/delete",
                    params: JSON.stringify(ids),
                    type: "POST",
                    contentType: "application/json",
                    successCallback: function () {
                        alert('操作成功', function (index) {
                            vm.reload();
                        });
                    }
                });
            });
        },
        getInfo: function (id) {
            Ajax.request({
                url: "../modulemaintainrecord/info/" + id,
                async: true,
                successCallback: function (r) {
                    vm.moduleMaintainRecord = r.moduleMaintainRecord;
                }
            });
        },
        reload: function (event) {
            vm.showList = true;
            var page = $("#jqGrid").jqGrid('getGridParam', 'page');
            $("#jqGrid").jqGrid('setGridParam', {
                postData: {'name': vm.q.name},
                page: page
            }).trigger("reloadGrid");
            vm.handleReset('formValidate');
        },
        reloadSearch: function () {
            vm.q = {
                name: ''
            };
            vm.reload();
        },
        handleSubmit: function (name) {
            handleSubmitValidate(this, name, function () {
                vm.saveOrUpdate()
            });
        },
        handleReset: function (name) {
            handleResetForm(this, name);
        },
        getModuleTypeTree: function () {
            //加载类型树
            Ajax.request({
                url: "../moduletype/select",
                async: true,
                successCallback: function (r) {
                    var tree = r.typeList;

                    moduleTypeTree = $.fn.zTree.init($("#moduleTypeTree"), setting, tree);

                    moduleTypeTree.expandAll(true);
                    var node = moduleTypeTree.getNodeByParam("id", vm.moduleMaintainRecord.moduleType);
                    if (node) {
                        moduleTypeTree.selectNode(node);
                        if (node.parentName) {
                            vm.moduleMaintainRecord.moduleTypeName = node.parentName;
                        } else {
                            vm.moduleMaintainRecord.moduleTypeName = '无';
                        }
                    } else {
                        node = moduleTypeTree.getNodeByParam("id", "0");
                        moduleTypeTree.selectNode(node);
                    }
                }
            });
        },
        typeTree: function () {
            openWindow({
                title: "选择模具类型",
                area: ['300px', '450px'],
                content: jQuery("#moduleTypeLayer"),
                btn: ['确定', '取消'],
                btn1: function (index) {
                    var node = moduleTypeTree.getSelectedNodes();
                    if (node.length != 0) {
                        vm.moduleMaintainRecord.moduleType = node[0].id;
                        vm.moduleMaintainRecord.moduleTypeName = node[0].typeName;
                    } else {
                        vm.moduleMaintainRecord.moduleType = 0;
                        vm.moduleMaintainRecord.moduleTypeName = '未选择';
                    }

                    layer.close(index);
                }
            });
        },
    }
});